using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;
using VISS.API.CustomUtils;

namespace VISS.API.Users {
	class UserFollowerDA {

        private const string SP_CountByFollowedUserKey = "dbo.[UserFollower_CountByFollowedUserKey]";
        private const string SP_CountByFollowerUserKey = "dbo.[UserFollower_CountByFollowerUserKey]";
        private const string SP_CheckExist = "dbo.[UserFollower_CheckExist]";
        private const string SP_Delete = "dbo.UserFollower_Delete";
        private const string SP_GetAll = "dbo.UserFollower_GetAll";
        private const string SP_GetByFollowerUserKey = "dbo.UserFollower_GetByFollowerUserKey";
        private const string SP_GetByFollowedUserKeyWithPaging = "dbo.[UserFollower_GetByFollowedUserKeyWithPaging]";
        private const string SP_Insert = "dbo.UserFollower_Insert";

		public static UserFollowerEnt CreateFromReader(IDataReader reader) {
			UserFollowerEnt item = new UserFollowerEnt();
            item.FollowerUserKey = ConvertUtils.ToString(reader["FollowerUserKey"]);
            item.FollowedUserKey = ConvertUtils.ToString(reader["FollowedUserKey"]);
            item.Name = ConvertUtils.ToString(reader["Name"]);
            item.Gender = (EUserGender)ConvertUtils.ToByte(reader["Gender"]);
            item.BirthDate = ConvertUtils.ToDateTime(reader["BirthDate"]);
            item.CityID = ConvertUtils.ToShort(reader["CityID"]);
            item.City = ConvertUtils.ToString(reader["City"]);
            item.CountryID = ConvertUtils.ToShort(reader["CountryID"]);
            item.Country = ConvertUtils.ToString(reader["Country"]);
            item.EducationID = ConvertUtils.ToShort(reader["EducationID"]);
            item.Education = ConvertUtils.ToString(reader["Education"]);
            item.JobID = ConvertUtils.ToShort(reader["JobID"]);
            item.Job = ConvertUtils.ToString(reader["Job"]);
            item.ImageCount = ConvertUtils.ToInt(reader["ImageCount"]);
            item.Blast = ConvertUtils.ToString(reader["Blast"]);
            item.AvatarFileName = ConvertUtils.ToString(reader["AvatarFileName"]);
            item.AvatarImageStoreID = ConvertUtils.ToInt(reader["AvatarImageStoreID"]);
            item.AvatarStatus = (EUserAvatarStatus)ConvertUtils.ToByte(reader["AvatarStatus"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			return item;
		}

        public static UserFollowerEnt CreateFromReaderForEvent(IDataReader reader) {
            EvtNoelUserFollowerEnt item = new EvtNoelUserFollowerEnt();
            item.FollowerUserKey = ConvertUtils.ToString(reader["FollowerUserKey"]);
            item.FollowedUserKey = ConvertUtils.ToString(reader["FollowedUserKey"]);
            item.Name = ConvertUtils.ToString(reader["Name"]);
            item.Gender = (EUserGender)ConvertUtils.ToByte(reader["Gender"]);
            item.BirthDate = ConvertUtils.ToDateTime(reader["BirthDate"]);
            item.CityID = ConvertUtils.ToShort(reader["CityID"]);
            item.City = ConvertUtils.ToString(reader["City"]);
            item.CountryID = ConvertUtils.ToShort(reader["CountryID"]);
            item.Country = ConvertUtils.ToString(reader["Country"]);
            item.EducationID = ConvertUtils.ToShort(reader["EducationID"]);
            item.Education = ConvertUtils.ToString(reader["Education"]);
            item.JobID = ConvertUtils.ToShort(reader["JobID"]);
            item.Job = ConvertUtils.ToString(reader["Job"]);
            item.ImageCount = ConvertUtils.ToInt(reader["ImageCount"]);
            item.Blast = ConvertUtils.ToString(reader["Blast"]);
            item.AvatarFileName = ConvertUtils.ToString(reader["AvatarFileName"]);
            item.AvatarImageStoreID = ConvertUtils.ToInt(reader["AvatarImageStoreID"]);
            item.AvatarStatus = (EUserAvatarStatus)ConvertUtils.ToByte(reader["AvatarStatus"]);
            item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
            item.GoOut = ConvertUtils.ToBool(reader["GoOut"]);
            return item;
        }

        public static int CountByFollowedUserKey(string followedUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountByFollowedUserKey)) {
                db.AddParameter(cmd, "FollowedUserKey", followedUserKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static int CountByFollowerUserKey(string followerUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountByFollowerUserKey)) {
                db.AddParameter(cmd, "FollowerUserKey", followerUserKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static int CheckExist(string followerUserKey, string followedUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CheckExist)) {
                db.AddParameter(cmd, "FollowerUserKey", followerUserKey);
                db.AddParameter(cmd, "FollowedUserKey", followedUserKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static int Delete(string followerUserKey, string followedUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Delete)) {
                db.AddParameter(cmd, "FollowerUserKey", followerUserKey);
                db.AddParameter(cmd, "FollowedUserKey", followedUserKey);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

		public static List<UserFollowerEnt> GetAll(string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserFollowerEnt> result = new List<UserFollowerEnt>();
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
                        UserFollowerEnt item = CreateFromReader(reader);
                        item.Age = Utils.CalculateAge(item.BirthDate);
                        result.Add(item);
					}
				}
			}
			return result;
		}

        public static List<UserFollowerEnt> GetByFollowerUserKey(string followerUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserFollowerEnt> result = new List<UserFollowerEnt>();
			Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByFollowerUserKey)) {
                db.AddParameter(cmd, "FollowerUserKey", followerUserKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
                        UserFollowerEnt item = CreateFromReaderForEvent(reader);
                        item.Age = Utils.CalculateAge(item.BirthDate);
                        result.Add(item);
					}
				}
			}
			return result;
		}

        public static List<UserFollowerEnt> GetByFollowedUserKeyWithPaging(string followedUserKey, long startRow, long endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserFollowerEnt> result = new List<UserFollowerEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByFollowedUserKeyWithPaging)) {
                db.AddParameter(cmd, "FollowedUserKey", followedUserKey);
                db.AddParameter(cmd, "StartRow", startRow);
                db.AddParameter(cmd, "EndRow", endRow);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        UserFollowerEnt item = CreateFromReaderForEvent(reader);
                        item.Age = Utils.CalculateAge(item.BirthDate);
                        result.Add(item);
                    }
                }
            }
            return result;
        }

        public static int Insert(UserFollowerEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
                db.AddParameter(cmd, "FollowerUserKey", item.FollowerUserKey);
                db.AddParameter(cmd, "FollowedUserKey", item.FollowedUserKey);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}